package com.qwc.blog.web.admin;


import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.qwc.blog.common.annotation.AdminController;
import com.qwc.blog.entity.Tag;
import com.qwc.blog.entity.dto.TagDto;
import com.qwc.blog.entity.dto.TagRequestDto;
import com.qwc.blog.service.ITagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 标签表 前端控制器
 * </p>
 *
 * @author qwc
 * @since 2022-01-24
 */
@AdminController("/tag")
public class TagController {

    @Autowired
    private ITagService tagService;

    @GetMapping("/page")
    public ResponseEntity<Object> page(TagRequestDto<Tag> dto){
        TagRequestDto<Tag> page = tagService.page(dto, Wrappers.<Tag>lambdaQuery()
                .like(CharSequenceUtil.isNotBlank(dto.getName()), Tag::getName, dto.getName()));
        return ResponseEntity.ok(page);
    }

    @PostMapping
    public ResponseEntity<Object> add(@Validated @RequestBody TagDto dto){
        Tag tag = new Tag();
        tag.setName(dto.getName());
        return ResponseEntity.ok(tagService.save(tag));
    }

    @DeleteMapping("{ids}")
    public ResponseEntity<Object> deleteByIds(@PathVariable @NotNull(message = "请选择删除数据") Integer[] ids){
        boolean b = tagService.removeTagAndArticleTagByIds(Arrays.asList(ids));
        return ResponseEntity.ok(b);
    }

    @GetMapping("/list")
    public ResponseEntity<Object> list(){
        List<Tag> list = tagService.list();
        return ResponseEntity.ok(list);
    }
}
